package cn.jbolt.ec.cloud;

import cn.jbolt.common.model.BecApp;
import cn.jbolt.ec.wx.GenService;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
//import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

public class QnService {


    public static String upload(String filepath){


        BecApp becApp = GenService.getApp();
        // 华南区
        Configuration cfg = new Configuration(Region.region2());
        UploadManager uploadManager = new UploadManager(cfg);

        String accessKey = becApp.getQnAk();
        String secretKey =becApp.getQnSk();
        String bucket = becApp.getQnBucket();

        String prefix = "http://"+becApp.getQnDomain()+"/";

        String[] strArray = filepath.split("\\.");
        int suffixIndex = strArray.length -1;
        String suffix =  strArray[suffixIndex];


        String key = "t"+System.currentTimeMillis()+"."+suffix;

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        System.out.println(upToken);
        try {
            Response response = uploadManager.put(filepath, key, upToken);
            System.out.println(response.bodyStream());
            System.out.println();
            //解析上传成功的结果
            //DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//            System.out.println(putRet.key);
//            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            ex.printStackTrace();
            return null;
    }
        return prefix+key;

    }






}